﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Software_Installer
{
    public partial class CurrentAction : Form
    {
        private Thread _UpdaterThread;
        private List<Label> _listoflabels;
        private bool _running;

        public CurrentAction()
        {
            InitializeComponent();
            _listoflabels = new List<Label>();
            _running = false;
            BuildGUI();
            InstallerInterFace.Instance.FormatList();
            Updater();


        }

        private void CurrentAction_Load(object sender, EventArgs e)
        {
            InstallerInterFace.Instance.RunInstalls();
        }
        private void BuildGUI()
        {
            //form height is 113
            //label hieght is 17 
            int starting_yPoint = 46;
            int lbl_hieght = 17;
            int starting_form_hieght = 113;
            int count = 1;
            foreach (string SoftwareName in InstallerInterFace.Instance.GetInstallList())
            {

                int ypos = starting_yPoint + (lbl_hieght * count);
                this.Height = starting_form_hieght + (lbl_hieght * count);
                System.Diagnostics.Debug.WriteLine(SoftwareName);
                Label templabel = new Label();
                templabel.AutoSize = true;
                templabel.Font = new System.Drawing.Font("OCR A Extended", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                templabel.Location = new System.Drawing.Point(12, ypos);
                templabel.Name = "lbl_" + SoftwareName.Split('#')[1];
                templabel.Size = new System.Drawing.Size(118, lbl_hieght);
                templabel.TabIndex = 1;
                templabel.Text = SoftwareName.Split('#')[1];
                this.Controls.Add(templabel);
                //templabel.Show();
                _listoflabels.Add(templabel);
                count++;
            }
        }

        private void Updater()
        {
            _running = true;
            _UpdaterThread = new Thread(Stikeout);
            _UpdaterThread.Name = "Updater Thread";
            _UpdaterThread.Start();

        }


        private void Stikeout()
        {
            System.Diagnostics.Debug.WriteLine("UpdaterStarted");

            
            while (_running)
            {
                //int tempcount = InstallerInterFace.Instance.newlist().Count;
                
                //if (tempcount != InstallerInterFace.Instance.newlist().Count)
                //{
                    foreach (Label label in _listoflabels)
                    {
                        System.Diagnostics.Debug.WriteLine("in the loop");
                        if (!InstallerInterFace.Instance.newlist().Contains(label.Text))
                        {
                            label.Invoke((MethodInvoker)delegate
                            {
                                label.Font = new System.Drawing.Font("OCR A Extended", 12F, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                            });

                        }
                    }
                //}
               if (InstallerInterFace.Instance.newlist().Count == 0)
                {
                    _running = false;
                }
            }
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            foreach (Label label in _listoflabels)
            {
                Thread.Sleep(3000);
                InstallerInterFace.Instance.Sendfinish(label.Text);
                System.Diagnostics.Debug.WriteLine(InstallerInterFace.Instance.newlist().Count);

                

            }

            
        }
    }
}
